/**
 * Vite configuaration file
 * https://vitejs.dev/config/
 */

import { defineConfig, Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import api from './api'
import ViteRestart from 'vite-plugin-restart'
import viteCompression from 'vite-plugin-compression' // 开启压缩
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'

const restart = (): Plugin =>
  ViteRestart({
    restart: ['api/**']
  })

const compression = (): Plugin =>
  // 开启压缩 [文档](https://github.com/anncwb/vite-plugin-compression/blob/main/README.zh_CN.md)
  viteCompression({
    verbose: true,
    disable: false,
    // filter:()=>{}, // 那些资源不压缩
    threshold: 1024 * 50, // 体积大于 threshold 才会被压缩,单位 b
    deleteOriginFile: false, // 压缩后是否删除源文件
    algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
    ext: '.gz' // 生成的压缩包后缀
  })

const uiresolve = (): Plugin =>
  Components({ resolvers: [NaiveUiResolver()] })

const apiServer = (): Plugin => ({
  name: 'api',
  configureServer: async server => {
    // mount mock server, `/api` is the base url
    server.middlewares.use('/api', api)
  }
})

export default defineConfig({
  plugins: [vue(), apiServer(), uiresolve(), compression(), restart()],
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          'naive-ui': ['naive-ui']
        }
      }
    }
  }
})
